package com.wd.tw.service;

import com.wd.tw.domain.entity.ElevatorInfo;
import com.wd.tw.domain.vo.EChartsLineData;
import com.wd.tw.domain.vo.EleRunState;
import com.wd.tw.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.wd.tw.constants.EleConstant.ELE_CROWD_NUM;
import static com.wd.tw.constants.EleConstant.ELE_PERSON_COUNT;

/**
 * @className: ReportService
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2025/1/8
 **/
@Service
@Slf4j
public class ReportService {

    @Resource
    ElevatorConfigService elevatorConfigService;

    @Resource
    BooleanCountService booleanCountService;

    /**
     * 获取电梯当前运行状态
     * @return
     */
    public Map<Integer, EleRunState> getCurRunState(){

        Map<Integer, EleRunState> stateMap = new HashMap<>();
        List<ElevatorInfo> elevatorInfoList = elevatorConfigService.getElevatorInfoList();

        Map<Long, Integer> eleStateMap = booleanCountService.getElevatorState();
        Map<Long, Long> eleRunTimeMap = booleanCountService.getEleRunTime();


        for (int i = 0; i <elevatorInfoList.size() ; i++) {
            ElevatorInfo elevatorInfo = elevatorInfoList.get(i);
            EleRunState eleRunState = new EleRunState();
            Integer pCount = ELE_PERSON_COUNT.get(elevatorInfo.getCameraId());
            if (pCount != null) {
                eleRunState.setPCount(pCount);
            }
            //运行次数
            Integer runTm = eleStateMap.get(elevatorInfo.getElevatorId());
            if(runTm!=null)
            {
                eleRunState.setRunTm(runTm);
            }

            //运行时长
            Long runTime = eleRunTimeMap.get(elevatorInfo.getElevatorId());
            if(runTime!=null)
            {
                long differenceInSeconds = TimeUtils.getCurrentTime() - runTime;
                     // 将秒数转换为分钟数，向下取整
                eleRunState.setRunTime((int) differenceInSeconds / 60);
            }

            stateMap.put(elevatorInfo.getElevatorId().intValue(), eleRunState);
        }

        return stateMap;
    }
}
